package com.chinaunicom.bigcustomer.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chinaunicom.bigcustomer.dao.BrandMapper;
import com.chinaunicom.bigcustomer.dto.GradeRuleDto;
import com.chinaunicom.bigcustomer.model.Brand;
import com.chinaunicom.bigcustomer.model.Grade;
import com.chinaunicom.bigcustomer.service.IBrandService;
import com.chinaunicom.bigcustomer.service.IGradeRuleService;
import com.chinaunicom.bigcustomer.service.IGradeService;

@Controller
@RequestMapping("/rule/info")
public class GradeRuleController extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Resource
	private IGradeRuleService graderuleService;
	
	@Resource
	private IBrandService brandService;
	
	@Resource
	private IGradeService gradeService;
	
	private GradeRuleDto gradeRuleDto;
	
/*
 * 查询所有的客户信息
 * 
 */
	@RequestMapping("/query")
	public String query(Model model) throws Exception{
		//查询所有的规则
		List<GradeRuleDto> list = graderuleService.query(null);
		model.addAttribute("list", list);
		//查询所有的品牌
		List<Brand> brandList = brandService.selectByPrimaryKey(null);
		model.addAttribute("brandList", brandList);
		//查询所有的等级
		List<Grade> gradeList = gradeService.selectByPrimaryKey(null);
		model.addAttribute("gradeList", gradeList);
		return "/WEB-INF/bigcustmer/rule/rulelist.jsp";
	}
	
	@RequestMapping("/queryByBrand")
	public String queryByBrand(Model model,HttpServletRequest request) throws Exception{
		String brandName = new String(request.getParameter("brandName").getBytes("iso-8859-1"), "utf-8");
		System.out.println(brandName);
/*		model.addAttribute("brandName",brandName);
		gradeRule.getvBrandname();*/
		List<GradeRuleDto> bList = graderuleService.queryByBrand(brandName);
		model.addAttribute("bList",bList);
		return "/WEB-INF/bigcustmer/rule/rulelist.jsp";
		}	
	
	
	
	
	@RequestMapping("/queryByGrade")
	public String queryByGrade(Model model,HttpServletRequest request) throws Exception{
		String gradeName = new String(request.getParameter("gradeName").getBytes("iso-8859-1"), "utf-8");
		/*model.addAttribute("brandName",brandName);
		gradeRule.getvBrandname();*/
		List<GradeRuleDto> gList = graderuleService.queryByGrade(gradeName);
		model.addAttribute("gList",gList);
		return "/WEB-INF/bigcustmer/rule/rulelist.jsp";
	}
	
	
	@RequestMapping("/queryByName")
	public String queryByName(Model model,HttpServletRequest request) throws Exception{

		String brandName = new String(request.getParameter("brandName").getBytes("iso-8859-1"), "utf-8");
		String gradeName = new String(request.getParameter("gradeName").getBytes("iso-8859-1"), "utf-8");
		GradeRuleDto dto = new GradeRuleDto();
		if ("0".equals(brandName)) {
			brandName=null;
			dto.setvBrandname(brandName);
		}else{
			dto.setvBrandname(brandName);
		};
		if ("0".equals(gradeName)) {
			gradeName=null;
			dto.setvGrade(gradeName);
			
		}else {
			dto.setvGrade(gradeName);
		};
		/*		model.addAttribute("brandName",brandName);
		gradeRule.getvBrandname();*/
		List<GradeRuleDto> list = graderuleService.queryByName(dto);
		model.addAttribute("list",list);
		//查询所有的品牌
		List<Brand> brandList = brandService.selectByPrimaryKey(null);
		model.addAttribute("brandList", brandList);
		//查询所有的等级
		List<Grade> gradeList = gradeService.selectByPrimaryKey(null);
		model.addAttribute("gradeList", gradeList);		
		
		return "/WEB-INF/bigcustmer/rule/rulelist.jsp";
	}	
	
	@RequestMapping("/toAddRule")
	public String toAddRule(Model model) throws Exception{
		//查询所有的品牌
		List<Brand> brandList = brandService.selectByPrimaryKey(null);
		model.addAttribute("brandList", brandList);
		//查询所有的等级
		List<Grade> gradeList = gradeService.selectByPrimaryKey(null);
		model.addAttribute("gradeList", gradeList);
		return "/WEB-INF/bigcustmer/rule/rule_add.jsp";
	}
	
	@RequestMapping("/save")
	public String save(Model model,GradeRuleDto dto,HttpServletRequest request) throws Exception{
		String money=request.getParameter("money");
		String month=request.getParameter("month");
		String cost=request.getParameter("cost");
		String month_intf=request.getParameter("month_intf");
		String intfs=request.getParameter("intfs");
		String nCondition1 = "一次性预存话费"+money+"元；连续"+month+"个月内，平均每个月消费"+cost+"月";
		dto.getGradeRule().setnCondition1(nCondition1);
		String nCondition2 = "连续"+month_intf+"个月内累积积分达到"+intfs+"以上";
		dto.getGradeRule().setnCondition2(nCondition2);
		String frozen = "正常";
		dto.getGradeRule().setvFrozen(frozen);
		System.out.println(dto);
		graderuleService.insert(dto);
		
		//根据已有的品牌和等级添加规则，不需要往品牌和等级表添加数据
/*		Integer brandid = dto.getGradeRule().getnBrandid();
		Integer gradeid = dto.getGradeRule().getnGradeid();
		if (brandid != null) {
			brandService.insert(null);
		};
		if (gradeid != null) {
			gradeService.insert(null);
		}*/
		//添加后跳回分级规则列表页面
		return "redirect:/rule/info/query";
	}
	
	
	
	@RequestMapping("/update")
	public String update(Model model,GradeRuleDto dto,String id,HttpServletRequest request) throws Exception{
		id = request.getParameter("id");
		System.out.println(id);
		String money=request.getParameter("money");
		String month=request.getParameter("month");
		String cost=request.getParameter("cost");
		String month_intf=request.getParameter("month_intf");
		String intfs=request.getParameter("intfs");
		String nCondition1 = "一次性预存话费"+money+"元；连续"+month+"个月内，平均每个月消费"+cost+"月";
		dto.getGradeRule().setnCondition1(nCondition1);
		String nCondition2 = "连续"+month_intf+"个月内累积积分达到"+intfs+"以上";
		dto.getGradeRule().setnCondition2(nCondition2);
		dto.getGradeRule().setnRuleno(Integer.parseInt(id));
		System.out.println(dto);
		graderuleService.updateById(dto);
		
		//根据已有的品牌和等级添加规则，不需要往品牌和等级表添加数据
		/*		Integer brandid = dto.getGradeRule().getnBrandid();
		Integer gradeid = dto.getGradeRule().getnGradeid();
		if (brandid != null) {
			brandService.insert(null);
		};
		if (gradeid != null) {
			gradeService.insert(null);
		}*/
		//添加后跳回分级规则列表页面
		return "redirect:/rule/info/query";
	}	
	
	@RequestMapping("/queryDetails")
	public String queryDetails(Model model,int id) throws Exception{
		//通过id获取单条分级规则的信息
		gradeRuleDto = graderuleService.selectByPrimaryKey(id);
		model.addAttribute("gradeRuleDto", gradeRuleDto);

		return "/WEB-INF/bigcustmer/rule/ruledetails.jsp";
	}
	
	
	@RequestMapping("/deleteRule")
	public String deleteRule(Model model,int id) throws Exception{
		graderuleService.deleteByPrimaryKey(id);
		
		return "redirect:/rule/info/query";
	}
	
	@RequestMapping("/lockRule")
	public String lockRule(Model model,int id) throws Exception{
		graderuleService.lockRuleByPrimaryKey(id);
		
		return "redirect:/rule/info/query";
	}
	
	@RequestMapping("/unlockRule")
	public String unlockRule(Model model,int id) throws Exception{
		graderuleService.unlockRuleByPrimaryKey(id);
		
		return "redirect:/rule/info/query";
	}
	
	
	@RequestMapping("/getInfoById")
	public String getInfoById(Model model,int id) throws Exception{
		//通过id获取单条分级规则的信息
		gradeRuleDto = graderuleService.selectByPrimaryKey(id);
		model.addAttribute("gradeRuleDto", gradeRuleDto);
		
		
		String condition1 = new String(gradeRuleDto.getGradeRule().getnCondition1());
		String condition2 = new String(gradeRuleDto.getGradeRule().getnCondition2());
		System.out.println(condition1);
		System.out.println(condition2);
		//取条件1的第一个数字
		int beginIndex1 = condition1.indexOf("费");
		int endIndex1 = condition1.indexOf("元");
		String money = condition1.substring(beginIndex1+1, endIndex1);
		//取条件1的第二个数字
		int beginIndex2 = condition1.indexOf("续");
		int endIndex2 = condition1.indexOf("个");
		String month = condition1.substring(beginIndex2+1, endIndex2);
		//取条件1的第三个数字
		int beginIndex3 = condition1.lastIndexOf("费");
		int endIndex3 = condition1.lastIndexOf("月");
		String cost = condition1.substring(beginIndex3+1, endIndex3);
		//取条件2的第一个数字
		int beginIndex4 = condition2.indexOf("续");
		int endIndex4 = condition2.indexOf("个");
		String month_intf = condition2.substring(beginIndex4+1, endIndex4);
		//取条件2的第二个数字
		int beginIndex5 = condition2.indexOf("到");
		int endIndex5 = condition2.indexOf("以");
		String intfs = condition2.substring(beginIndex5+1, endIndex5);
		System.out.println(money);
		System.out.println(month);
		System.out.println(cost);
		System.out.println(month_intf);
		System.out.println(intfs);
		//把值传给jsp页面
		model.addAttribute("money", money);
		model.addAttribute("month", month);
		model.addAttribute("cost", cost);
		model.addAttribute("month_intf", month_intf);
		model.addAttribute("intfs", intfs);
		
		return "/WEB-INF/bigcustmer/rule/rule_update.jsp";
	}
	
	

}
